<template>
	<view class="main">
		<scroll-view class="letter-scroll" :scroll-into-view="toView"  scroll-y="true" scroll-with-animation="true" :style="{height:clientHeight+'px'}">
			<!-- 头部固定搜索 -->
			<view class="header-search">
				<!-- 左边icon -->
				<view class="flex1 mb15" @click="tocity()">
					<view class="icon">
						<image class="icon-position" src="../../static/assets/index/icon-position.png"></image>
					</view>
					<text class="eclips">{{city}}</text>
				</view>
				<!-- 右边搜索 -->
				<view class="flex2 mb15">
					<view class="search-icon">
						<image src="../../static/assets/index/icon-serch.png"></image>
					</view>
					<input @confirm="confirm" class="search-input" confirm-type="search" v-model="searchVal" type="text" placeholder="输入车辆的名称或品牌" />
				</view>
			</view>

			<!-- banner -->
			<view class="banner" >
				<swiper indicator-dots="true" indicator-color="rgba(255,255,255,1)" indicator-active-color="#E41805" autoplay="true"
				 interval="3000">
					<swiper-item v-for="(item,index) in bannerList" :key="index">
						<image lazy-load="true"  :src="item.image"></image>
					</swiper-item>
				</swiper>
			</view>

			<!-- 推荐品牌 -->
			<view class="brand">
				<scroll-view scroll-x="true">
					<swiper class="brand-car-list" display-multiple-items=4.5>
						<swiper-item v-for="(item,key) in carType" :key="key" class="brand-car" @click="toBrand(item.id,item.name)" :data-id="item.id" :data-name="item.name">
							<view class="icon-main">
								<image lazy-load="true"  class="icon-logo" mode="aspectFit" :src="item.logo_image"></image>
							</view>
							<text>{{item.name}}</text>
						</swiper-item>
					</swiper>
				</scroll-view>
			</view>
			<!-- 猜你喜欢 -->
			<view class="recommend">
				<view class="recommend-title">热门车系</view>
				<view class="recommend-car-list">
					<view @click="toCarDetail(item.id)" :data-id="item.id" class="single-car" v-for="(item,index) in like" :key="index">
						<view class="single-car-img">
							<image lazy-load="true"  mode=" aspectFill" :src="item.image"></image>
						</view>
						<view class="words">{{item.name}}</view>
					</view>
				</view>
			</view>
			
			<!-- 网约车 -->
			<view class="recommend">
				<view class="recommend-title">网约车</view>
				<view class="recommend-car-list">
					<view @click="toSingle(item.id)" :data-id="item.id" class="single-car" v-for="(item,index) in net">
						<view class="single-car-img">
							<image lazy-load="true"  mode=" aspectFill" :src="item.image"></image>
						</view>
						<view class="words">{{item.name}}</view>
					</view>
				</view>
			</view>
			
			
			<!-- 字母查找 -->
			<view :id="index" class="scroll-view-item" v-for="(item,index) in scrollBrand">
				<view class="letter-title">{{index}}</view>
				<view class="letter-record" v-for="(list, cindex) in item" :key="index" @click="toBrand(list.id,list.name)" :data-id="list.id" :data-name="list.name">
					<view class="letter-img">
						<image mode="aspectFit" lazy-load="true"  class="icon-logo-list" :src="list.logo_image"></image>
					</view>
					<view class="letter-name">{{list.name}}</view>
				</view>
			</view>
		</scroll-view>
		<!-- 字母导航 -->
		<view class="navigation-bars">
			<view class="navigation-bars-initial " :class="letterActive==item?'active':''" v-for="(item,index) in initial" :key="index"
			 @click="goToNode(item)">
				{{item}}
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				searchVal: '',
				city: "成都",
				bannerList: [], //banner图片
				carType: [],
				like: [], //猜你喜欢
				initial: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
					'V', 'W', 'X', 'Y', 'Z'
				], //字母导航
				scrollBrand: [], //品牌列表
				toView: '', //跳转id
				letterActive: '', //字母选中状态
				params: {},
				ifOnShow: false,
				net:[],
				clientHeight:0
			}
		},
		onLoad() {
			let _self = this;
			_self.getIndex();
			_self.brandList();
			uni.getStorage({
				key: "position",
				success(e) {
					_self.city = e.data
				}
			})
			uni.getSystemInfo({
				success: function(res) {
					let height = res.windowHeight
					_self.clientHeight = height
				}
			});
		},
		onHide() {
			this.ifOnShow = true
		},
		onShow() {
			let _self = this
			if (this.ifOnShow) {
				uni.getStorage({
					key: "position",
					success(e) {
						_self.city = e.data
					}
				})
			}
		},
		methods: {
			/* */
			tocity() {
				uni.navigateTo({
					url: '../component/city-model/city?type=3'
				})
			},
			/**
			 * 去到对应的节点
			 */
			goToNode: function(val) {
				let that = this;
				var hash = val;
				that.toView = hash;
				that.letterActive = hash
			},
			/*网约车*/
			tocheap(){
				uni.navigateTo({
					url:'../list/cheap/cheap'
				})
			},
			/*去品牌页*/
			toBrand(id, name,type) {
				if(type="net"){
					uni.navigateTo({
						url: '../list/brand/brand?brand_id=' + id + '&name=' + name
					})
				}else{
					uni.navigateTo({
						url: '../list/brand/brand?brand_id=' + id + '&name=' + name
					})
				}
			},
			/*去车系页*/
			toCarDetail(id) {
				uni.navigateTo({
					url: '../detail/carDetail/carDetail?v_id=' + id + '&city=' + this.city
				})
			},
			/**单个车型明细*/
			toSingle(id) {
				uni.navigateTo({
					url: '../detail/carSingle/carSingle?cid=' + id 
				})
			},
			/*搜索框*/
			confirm() {
				let _self = this,
					params = {};
				params.search = _self.searchVal
				_self.$minApi.search(params).then(res => {
					if (res.code == 2001) {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						});
					} else if (res.code == 1) {
						let url, id = res.data.id;
						if (res.data.action == 'brandVList') {
							url = '../list/brand/brand?brand_id='
						} else {
							url = '../detail/carDetail/carDetail?v_id='
						}
						uni.navigateTo({
							url: url + id
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			},
			/*品牌列表数据*/
			brandList(){
				let _self = this
				_self.$minApi.brandlist().then(res => {
					_self.scrollBrand = res.data.brand
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			},
			/*首页数据*/
			getIndex: function() {
				let _self = this
				_self.$minApi.index({params:_self.city}).then(res => {
					_self.bannerList = res.data.ad;
					_self.carType = res.data.index_brand
					_self.like = res.data.like
					_self.net = res.data.net
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					})
				})
			},
		}
	}
</script>

<style scoped lang="less">
	.main {
		height: 100vh;

		// overflow-y: auto;
		.header-search {
			width: 100%;
			height: 88rpx;
			background-color: #E41805;
			position: relative;
			top: 0;
			left: 0;
			display: flex;
			align-items: flex-end;
			justify-items: center;
			z-index: 99;
		}
		.words {
			word-break: break-all;
		}
		.flex{
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.flexright{
			color: #999;
			font-weight: normal;
			font-size: 24rpx;
		}
		.iconrigth{
			font-size: 20rpx;
			margin-left: 16rpx;
		}
		.flex1 {
			text-align: center;
			color: #fff;
			width: 95rpx;
			height: 64rpx;
			font-size: 20rpx;
		}
		.eclips {
			display: inline-block;
			width: 60rpx;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.mb15 {
			margin: 0 0 15rpx;
		}

		.icon {
			display: block;
		}

		.icon-position {
			width: 24rpx;
			margin: 0 0 6rpx;
			height: 30rpx;
		}

		.flex2 {
			flex: 2;
			margin: 0 30rpx 15rpx 15rpx;
			height: 64rpx;
			width: 100%;
			background: #fff;
			font-size: 28rpx;
			display: flex;
			border-radius: 10rpx;
			align-items: center;
		}

		.search-input {
			display: inline-block;
			height: 64rpx;
			width: 88%;
			padding-left: 16rpx;
			background: transparent;
			outline: none;
			vertical-align: middle;
		}

		.search-icon {
			display: block;
			width: 56rpx;
		}

		.search-icon image {
			width: 30rpx;
			height: 30rpx;
			margin: 18rpx 16rpx 16rpx 25rpx;
		}

		/* banner */
		.banner {
			width: 690rpx;
			height: 345rpx;
			margin: 30rpx 30rpx 0;
			border-radius: 20rpx;
		}

		.banner swiper {
			width: 100%;
			height: 100%;
		}

		.banner image {
			width: 100%;
			height: 100%;
			border-radius: 20rpx;
		}

		/* 模块分类 */

		.icon-hot {
			width: 80rpx;
			height: 80rpx;
		}

		.icon-logo {
			width: 150rpx;
			height: 80rpx;
		}


		/* 猜你喜欢 */
		.brand {
			margin: 30rpx 0rpx 30rpx 30rpx;
			color: #333;
		}

		.brand-car-list {
			background: #fff;
			height: 140rpx;
			display: flex;
			text-align: center;
			justify-content: space-between;
			font-size: 24rpx;
		}

		.brand-car {
			flex: 2;
			margin-right: 10rpx;
		}


		/* 猜你喜欢 */

		.recommend {
			padding: 30rpx;
			color: #333;
		}

		.recommend-title {
			font-size: 30rpx;
			margin: 0 18rpx;
			font-weight: bold;
		}

		.recommend-car-list {
			display: flex;
			align-content: flex-start;
			text-align: center;
			font-size: 24rpx;
			justify-items: left;
			flex-wrap: wrap;
		}

		.single-car {
			width: 180rpx;
			height: 150rpx;
			margin-top: 30rpx;
		}

		.single-car:nth-child(2),
		.single-car:nth-child(5) {
			margin: 30rpx 56rpx 0;
		}

		.single-car-img image {
			width: 180rpx;
			height: 96rpx;
		}

		/* 字母导航 */
		.navigation-bars {
			z-index: 2;
			position: fixed;
			top: 210rpx;
			right: 6rpx;
			width: 30rpx;
		}

		.navigation-bars-initial {
			display: flex;
			width: 30rpx;
			height: 30rpx;
			justify-content: center;
			align-items: center;
			color: #999;
			margin: 12rpx auto;
			font-size: 24rpx;
			border-radius: 50%;
		}

		.navigation-bars-initial.active {
			background: #E41805;
			color: #fff;
		}

		.letter-scroll {
			position: relative;
			height: 100vh;
			overflow: hidden;
		}

		.scroll-view-item {
			height: auto;
			// padding-top: 80rpx;
		}
		// .scroll-view-item .letter-record:last-child{
		// 	margin-bottom: 0!important;
		// }

		.letter-title {
			background: #f8f8f8;
			color: #969696;
			font-size: 20rpx;
			padding-left: 30rpx;
			margin-bottom: 40rpx;
		}

		.letter-record {
			display: flex;
			height: 65rpx;
			margin-bottom: 40rpx;
			align-items: center;
		}

		.letter-img {
			margin: 0 50rpx;
			width: 95rpx;
			height: 65rpx;
			text-align: center;
		}

		.letter-img image {
			width: 100%;
			height: 100%;
		}

		.icon-logo-list {
			width: 96px;
			height: 66rpx;
		}

		.letter-name {
			font-size: 28rpx;
			color: #333;
		}
	}
</style>
